package com.yb.rxjava.map;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;

import com.yb.rxjava.R;
import com.yb.rxjava.databinding.ActivityMapBinding;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class MapActivity extends AppCompatActivity {

    private static final String TAG = MapActivity.class.getSimpleName();

    private ActivityMapBinding mapBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapBinding = ActivityMapBinding.inflate(LayoutInflater.from(this));
        setContentView(mapBinding.getRoot());
        mapBinding.mBtMap.setOnClickListener(v -> {
            Observable.create((ObservableOnSubscribe<String[]>) emitter -> {
                emitter.onNext(new String[]{"This", "is", "RxJava"});
                emitter.onComplete();
            })
                    .map(strings -> {
                        List<String> mapStr = new ArrayList<>();
                        Collections.addAll(mapStr, strings);
                        return mapStr;
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(o ->
                            mapBinding.mTvMapResult.setText("数据改变的结果："+o.toString()));
        });
    }
}